package com.linxz.wandnroid.module;


import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.module.BaseModel;
import com.linxz.framework.protocol.callback.HttpCallBack;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.ui.pojo.BannerBean;
import com.linxz.wandnroid.pojo.BannerAndArticles;
import com.linxz.wandnroid.pojo.Category;
import com.linxz.wandnroid.pojo.WanAndroidArticleBean;
import com.linxz.wandnroid.pojo.WandroidBannerBean;
import com.linxz.wandnroid.pojo.WandroidProjectBean;
import com.linxz.wandnroid.pojo.resp.GankResp;
import com.linxz.wandnroid.pojo.resp.SystemPageData;
import com.linxz.wandnroid.pojo.resp.WanAndroidArticleListResp;
import com.linxz.wandnroid.pojo.resp.WanAndroidNavigationListResp;
import com.linxz.wandnroid.pojo.resp.WanTopArticleListResp;
import com.linxz.wandnroid.pojo.resp.WandroidBannerListResp;
import com.linxz.wandnroid.pojo.resp.WandroidProjectCategoryResp;
import com.linxz.wandnroid.pojo.resp.WandroidProjectListResp;
import com.linxz.wandnroid.protocol.WanAndroidApiStore;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function3;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2020年02月20日 23:29
 * version：v4.5.4
 * 描述：
 */
public class WanAndroidModule extends BaseModel {

    private WanAndroidApiStore mWanAndroidApiStore;

    public WanAndroidModule(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(lifecycleSubject);
        mWanAndroidApiStore=getOutApiStore(WanAndroidApiStore.class);
    }

    /**获取Banners数据*/
    public void getBanners(final ModelCallBack<List<BannerBean>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getWandroidBanner(),new HttpCallBack<WandroidBannerListResp>(){
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WandroidBannerListResp wandroidBannerListResp) {
               if (wandroidBannerListResp.getData()==null || wandroidBannerListResp.getData().size()==0){
                   modelCallBack.onSuccess(new ArrayList<BannerBean>());
               }else{
                   List<BannerBean> bannerBeans=new ArrayList<>();
                   for(WandroidBannerBean wanBanner:wandroidBannerListResp.getData()){
                       BannerBean bannerBean=new BannerBean();
                       bannerBean.setImgUrl(wanBanner.getImagePath());
                       bannerBean.setLinkUrl(wanBanner.getUrl());
                       bannerBean.setLinkCategory("c10001");
                       bannerBeans.add(bannerBean);
                   }
                   modelCallBack.onSuccess(bannerBeans);
               }
            }
        });
    }
    /**
     * 刷新文章页面：Banner/置顶/文章都刷新成功后再返回
     * */
    public void refreshArticle(final ModelCallBack<BannerAndArticles> modelCallBack){
        Observable.zip(mWanAndroidApiStore.getWandroidBanner(),mWanAndroidApiStore.getTopArticle(),mWanAndroidApiStore.getArticleList(0),new Function3<WandroidBannerListResp, WanTopArticleListResp, WanAndroidArticleListResp, BannerAndArticles>() {
            @Override
            public BannerAndArticles apply(WandroidBannerListResp wandroidBannerListResp,WanTopArticleListResp topArticles, WanAndroidArticleListResp pageArticles) {
                List<BannerBean> bannerBeans=new ArrayList<>();
                for(WandroidBannerBean wanBanner:wandroidBannerListResp.getData()){
                    BannerBean bannerBean=new BannerBean();
                    bannerBean.setImgUrl(wanBanner.getImagePath());
                    bannerBean.setLinkUrl(wanBanner.getUrl());
                    bannerBean.setLinkCategory("c10001");
                    bannerBeans.add(bannerBean);
                }

                List<WanAndroidArticleBean> list=new ArrayList<>();
                if (topArticles.isOk()){
                    list.addAll(topArticles.getData());
                }
                if (pageArticles.isOk()){
                    list.addAll(pageArticles.getData().getDatas());
                }

                return new BannerAndArticles(bannerBeans,list);

            }

        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BannerAndArticles>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BannerAndArticles list) {
                modelCallBack.onSuccess(list);
            }

            @Override
            public void onError(Throwable e) {
                modelCallBack.onErrors("-9999",e.getMessage());
            }

            @Override
            public void onComplete() {
            }
        });

    }

    /**
     * 文章推荐列表
     * */
    public void getArticleList(int page, final ModelCallBack<List<WanAndroidArticleBean>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getArticleList(page), new HttpCallBack<WanAndroidArticleListResp>() {

            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WanAndroidArticleListResp wanAndroidArticleListResp) {
                if (wanAndroidArticleListResp.getData()!=null && wanAndroidArticleListResp.getData().getDatas()!=null){
                    modelCallBack.onSuccess(wanAndroidArticleListResp.getData().getDatas());
                }else{
                    modelCallBack.onErrors(wanAndroidArticleListResp.getErrorCode()+"",wanAndroidArticleListResp.getErrorMsg());
                }

            }
        });
    }

    /**置顶文章列表*/
    public void getTopArticle(final ModelCallBack<List<WanAndroidArticleBean>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getTopArticle(), new HttpCallBack<WanAndroidArticleListResp>() {

            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WanAndroidArticleListResp wanAndroidArticleListResp) {
                if (wanAndroidArticleListResp.getData()!=null && wanAndroidArticleListResp.getData().getDatas()!=null){
                    modelCallBack.onSuccess(wanAndroidArticleListResp.getData().getDatas());
                }else{
                    modelCallBack.onErrors(wanAndroidArticleListResp.getErrorCode()+"",wanAndroidArticleListResp.getErrorMsg());
                }

            }
        });
    }


    /**获取Banner/导航数据数据*/
    public void getBannerAndNavigations(final ModelCallBack<SystemPageData> modelCallBack){
        Observable.zip(mWanAndroidApiStore.getWandroidBanner(),mWanAndroidApiStore.getNavigations(),new BiFunction<WandroidBannerListResp, WanAndroidNavigationListResp, SystemPageData>() {
            @Override
            public SystemPageData apply(WandroidBannerListResp bannerListResp, WanAndroidNavigationListResp wanAndroidNavigationListResp) {
                SystemPageData systemPageData=new SystemPageData();
                systemPageData.setBanners(bannerListResp.getData());
                systemPageData.setNavigationBeans(wanAndroidNavigationListResp.getData());
                return systemPageData;
            }

        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<SystemPageData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SystemPageData data) {
                modelCallBack.onSuccess(data);
            }

            @Override
            public void onError(Throwable e) {
                modelCallBack.onErrors("-9999",e.getMessage());
            }

            @Override
            public void onComplete() {
            }
        });
    }


    /**获取项目分类*/
    public void getProjectCategory(final ModelCallBack<List<Category>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getWandroidProjectCategory(),new HttpCallBack<WandroidProjectCategoryResp>(){
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WandroidProjectCategoryResp wandroidProjectCategoryResp) {
               modelCallBack.onSuccess(wandroidProjectCategoryResp.getData());
            }
        });
    }

    /**获取项目列表*/
    public void getProjectList(int cid, int page, final ModelCallBack<List<WandroidProjectBean>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getWandroidProjects(page,cid),new HttpCallBack<WandroidProjectListResp>(){
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WandroidProjectListResp wandroidProjectListResp) {
               modelCallBack.onSuccess(wandroidProjectListResp.getData().getData());
            }
        });
    }


    /**获取分类下的文章列表*/
    public void getCategoryArticles(int cid,int page,final ModelCallBack<List<WanAndroidArticleBean>> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getWanAndroidCategoryArticles(page,cid),new HttpCallBack<WanAndroidArticleListResp>(){
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(WanAndroidArticleListResp wanAndroidArticleListResp) {
               modelCallBack.onSuccess(wanAndroidArticleListResp.getData().getDatas());
            }
        });
    }

    public void getGankList(final ModelCallBack<GankResp> modelCallBack){
        toSubscribe(mWanAndroidApiStore.getGank(),new HttpCallBack<GankResp>(){
            @Override
            public void onErrors(String code, String msg) {
                modelCallBack.onErrors(code,msg);
            }

            @Override
            public void onResponse(GankResp gankResp) {
               modelCallBack.onSuccess(gankResp);
            }
        });
    }


}
